Skip to content

Method: setProperty(ConfigurationParameter, String)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.config;
19:
20: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
21:
22: import java.util.Collection;
23: import java.util.HashMap;
24: import java.util.Map;
25: import java.util.Objects;
26:
27: /**
28: * Holds configuration of the OntoDriver.
29: */
30: public final class DriverConfiguration {
31:
32: private final Map<ConfigurationParameter, String> configuration = new HashMap<>();
33: private final OntologyStorageProperties storageProperties;
34:
35: public DriverConfiguration(OntologyStorageProperties storageProperties) {
36: this.storageProperties = Objects.requireNonNull(storageProperties);
37: }
38:
39: /**
40: * Loads configuration of parameters specified by {@code parameters} from the provided properties map.
41: *
42: * @param properties Map of configuration values
43: * @param parameters Parameters to extract from the map
44: */
45: public void addConfiguration(Map<String, String> properties, Collection<ConfigurationParameter> parameters) {
46: Objects.requireNonNull(properties);
47: Objects.requireNonNull(parameters);
48: parameters.stream().filter(p -> properties.containsKey(p.toString()))
49: .forEach(p -> setProperty(p, properties.get(p.toString())));
50: }
51:
52: /**
53: * Sets configuration of the specified parameter to the specified value.
54: * <p>
55: * Overrides any previous setting.
56: *
57: * @param property Parameter
58: * @param value Value
59: */
60: public void setProperty(ConfigurationParameter property, String value) {
61: Objects.requireNonNull(property);
62: configuration.put(property, value);
63: }
64:
65: /**
66: * Gets value of the specified property.
67: *
68: * @param property Parameter
69: * @return Value of the property or {@code null}, if it is not set
70: */
71: public String getProperty(ConfigurationParameter property) {
72: Objects.requireNonNull(property);
73: return configuration.get(property);
74: }
75:
76: /**
77: * Gets value of the specified property or the default value, if the property is not set.
78: *
79: * @param property Parameter
80: * @param defaultValue Value to return if the property is not set
81: * @return Value of the property or {@code defaultValue}, if it is not set
82: */
83: public String getProperty(ConfigurationParameter property, String defaultValue) {
84: Objects.requireNonNull(property);
85: return configuration.getOrDefault(property, defaultValue);
86: }
87:
88: /**
89: * Gets integer value of the specified property or the default value, if the property is not set.
90: *
91: * @param property Parameter
92: * @param defaultValue Value to return if the property is not set
93: * @return Value of the property or {@code defaultValue}, if it is not set
94: * @throws IllegalArgumentException If the configured property value is not an integer
95: */
96: public int getProperty(ConfigurationParameter property, int defaultValue) {
97: final String propertyValue = getProperty(property, Integer.toString(defaultValue));
98: try {
99: return Integer.parseInt(propertyValue);
100: } catch (NumberFormatException e) {
101: throw new IllegalArgumentException("Value '" + propertyValue + "' of property '" + propertyValue + "' is not a valid integer.", e);
102: }
103: }
104:
105: /**
106: * Returns value of the specified property as boolean.
107: * <p>
108: * If the property is not configured, {@code false} is returned.
109: *
110: * @param property Parameter
111: * @return Property value (false for unknown)
112: */
113: public boolean is(ConfigurationParameter property) {
114: Objects.requireNonNull(property);
115: return Boolean.parseBoolean(configuration.get(property));
116: }
117:
118: /**
119: * Checks whether the specified property is set in this configuration.
120: *
121: * @param property The property to check
122: * @return Whether the specified property is set here
123: */
124: public boolean isSet(ConfigurationParameter property) {
125: return configuration.containsKey(property);
126: }
127:
128: public OntologyStorageProperties getStorageProperties() {
129: return storageProperties;
130: }
131:
132: @Override
133: public boolean equals(Object o) {
134: if (this == o) {
135: return true;
136: }
137: if (o == null || getClass() != o.getClass()) {
138: return false;
139: }
140:
141: DriverConfiguration that = (DriverConfiguration) o;
142:
143: return configuration.equals(that.configuration) && storageProperties.equals(that.storageProperties);
144:
145: }
146:
147: @Override
148: public int hashCode() {
149: int result = configuration.hashCode();
150: result = 31 * result + storageProperties.hashCode();
151: return result;
152: }
153: }